package com.ywm.eduService.controller;




import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywm.commonUtil.R;
import com.ywm.eduService.entity.EduTeacher;
import com.ywm.eduService.entity.vo.TeacherQuery;
import com.ywm.eduService.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-12
 */
@RestController
@RequestMapping("/eduService/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping
    public R list(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);
    }
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/{id}")
    public R removeById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
        boolean b = eduTeacherService.removeById(id);
        if (b) {
            return R.ok();
        }else {
            return R.error();
        }

    }

    /**
     * 分页查询讲师
     * @param current 当前页
     * @param limit 每页显示的数量
     * @return
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,@PathVariable long limit){
        //创建page对象
        Page<EduTeacher> teacherPage=new Page<>(current,limit);
        //调用方法实现分页
        //调用方式时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        eduTeacherService.page(teacherPage,null);
        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();//数据list集合
        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 条件分页查询
     * @param current 当前页
     * @param limit 每页显示数
     * @param teacherQuery 查询条件
     * @return
     */
    @ApiOperation(value = "条件分页查询")
    @RequestMapping("/pageTeacherCondition/{current}/{limit}")
    public R teacherQuery(@PathVariable long current, @PathVariable long limit, @RequestBody TeacherQuery teacherQuery){
        Page<EduTeacher> teacherPage=new Page<>(current,limit);
        //构件条件
        QueryWrapper<EduTeacher> queryWrapper=new QueryWrapper<>();
        if (teacherQuery != null) {
            //多条件组合查询（用动态sql实现）
            String name = teacherQuery.getName();
            Integer level = teacherQuery.getLevel();
            String begin = teacherQuery.getBegin();
            String end = teacherQuery.getEnd();
            if (!StringUtils.isEmpty(name)) {
                queryWrapper.like("name", name);//模糊查询
            }

            if (!StringUtils.isEmpty(level) ) {
                queryWrapper.eq("level", level);//判断等级
            }

            if (!StringUtils.isEmpty(begin)) {
                queryWrapper.ge("gmt_create", begin);//大于begin
            }

            if (!StringUtils.isEmpty(end)) {
                queryWrapper.le("gmt_create", end);//小于end
            }
        }
        //调用方式时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        eduTeacherService.page(teacherPage,queryWrapper);
        long total = teacherPage.getTotal();//查询的总数
        List<EduTeacher> records = teacherPage.getRecords();//数据list集合
//        System.out.println(records.get(0).getGmtCreate());
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher){
        System.out.println(teacher);
        boolean save = eduTeacherService.save(teacher);
        if (save) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 根据ID查询讲师
     */
    @ApiOperation("根据ID查询讲师")
    @GetMapping("/getTeacher/{id}")
    public R queryTeacherById(@PathVariable long id){
        EduTeacher byId = eduTeacherService.getById(id);
        if (byId == null) {
            return R.error();
        }
        return R.ok().data("teacher",eduTeacherService.getById(id));
    }
    /**
     * 讲师修改功能
     */
    @PostMapping("/updateTeacher")
    @ApiOperation("根据ID更新讲师")
    public  R updateTeacherByID(@RequestBody EduTeacher teacher){
        boolean b = eduTeacherService.updateById(teacher);
        if (b) {
            return R.ok();
        }
        return R.error();
    }
}

